<jsp:useBean id="sessiondata" class="bibbletex.models.SessionData"
	scope="session" />

<%@ page
	import="bibbletex.models.*,java.util.UUID,java.util.List, java.util.ArrayList"
	import="bibbletex.views.CollaborationView,bibbletex.controllers.CollaborationController"
	import="bibbletex.helpers.BibtexFormat, bibbletex.helpers.FileHelper" %>	

<%!
	String page_title = new String("Export references");
	String onloadjsfunction = "";
	boolean show_title = true;
	String[] jstitle={"collab"};
	boolean show_navigation = true;
%>
<%
	String id_parameter = request.getParameter("cid");
	String file_parameter = request.getParameter("file");
	String[] rid_parameter = request.getParameterValues("rid");
	Collaboration c = null;
	
	if (id_parameter == null)
	{
		if (sessiondata.collaboration != null)
			c = sessiondata.collaboration;
	}
	else
	{
		c = CollaborationController.load(id_parameter);
		sessiondata.collaboration = c;
	}
	
	List<Reference> refs = new ArrayList<Reference>();
	List<Reference> crefs = c.getReferences(); 
	String rid_parameter_string = "";
	
	if (rid_parameter != null && rid_parameter.length > 0){
		for (String rid : rid_parameter){
			rid_parameter_string += "&rid="+rid;			
			if (crefs.contains(new Reference(UUID.fromString(rid)))){
				Reference r = crefs.get(crefs.indexOf(new Reference(UUID.fromString(rid))));
				refs.add(r);
			}
		}
	}
	else {
		refs = crefs;
	}
	
	if (c != null && file_parameter != null){
		FileHelper.writeFile(refs, response);
	} else {
%>
<%@ include file="/header.jsp"%>
<div id="Content">
	<div id="ContentCenter" class="clearfix">
<% if (c != null) { %>
<div id="exportContainer">
<div class="exportCode">
<h3>Copy BibTex-Code</h3>
<label for="bibtexcode"></label>
<textarea name="bibtexcode" rows="10">
<% for (Reference r : refs) { %>
<%=BibtexFormat.toString(r) %>
<% } %>
</textarea>
</div>
<div class="exportFile">
	<h3>Download as a File</h3>
	<a href="export.jsp?file<%=rid_parameter_string%>">Download BibTex-File</a>
</div>
</div>


<% } else { %>

<div class="error">Unable to load Collaboration.
<br />Please Contact our Support.</div>

<% } %>
</div> </div>
<%@ include file="/footer.jsp"%>
<% } %>
